package com.yunhe.policyfulfillment.policyful.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.policyful.domain.PolicyCommonQuestion;
import com.yunhe.policyfulfillment.policyful.model.PolicyCommonQuestionVo;
import com.yunhe.policyfulfillment.policyful.model.PolicyCommonQuestionVo2;
import com.yunhe.policyfulfillment.policyful.repository.PolicyCommonQuestionRepository;
import com.yunhe.policyfulfillment.policyful.service.PolicyCommonQuestionService;
import com.yunhe.policyfulfillment.util.DateTimeUtils;
import com.yunhe.policyfulfillment.util.MyHeaderUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhengbo
 * @date 2021/10/12 18:01
 */
@Service
public class PolicyCommonQuestionServiceImpl  implements PolicyCommonQuestionService {

    @Autowired
    private PolicyCommonQuestionRepository policyCommonQuestionRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;


    @Override
    public void createPolicyCommonQuestion (PolicyCommonQuestion policyCommonQuestion) throws ArgumentErrorException {
        if (StringUtils.isEmpty(policyCommonQuestion.getCommonProblem())) {
            throw new ArgumentErrorException("问题不能为空");
        }

        PolicyUser headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser != null){
            policyCommonQuestion.setPublisherId(headerUser.getId());
        }

        String releaseTime = policyCommonQuestion.getReleaseTime();
        //如果当前时间大于发布时间则为正常，反之是禁用状态
        if (DateTimeUtils.compareDateStr(releaseTime, DateTimeUtils.getCurDate("yyyy-MM-dd HH:mm:ss")) >= 0) {
            policyCommonQuestion.setState("1");
        } else {
            policyCommonQuestion.setState("0");
        }

        policyCommonQuestionRepository.createPolicyCommonQuestionSelective(policyCommonQuestion);
    }

    @Override
    public void patchDeletePolicyCommonQuestion(List<Long> ids) {
        for (Long id : ids) {
            policyCommonQuestionRepository.deletePolicyCommonQuestionByPrimaryKey(id.intValue());
        }
    }


    @Override
    public void deletePolicyCommonQuestion(int id) {
        policyCommonQuestionRepository.deletePolicyCommonQuestionByPrimaryKey(id);
    }


    @Override
    public void updatePolicyCommonQuestion(PolicyCommonQuestion policyCommonQuestion) {
        policyCommonQuestionRepository.updatePolicyCommonQuestionByPrimaryKeySelective(policyCommonQuestion);
    }


    @Override
    public PolicyCommonQuestion findPolicyCommonQuestionById(int id) {
        return policyCommonQuestionRepository.searchPolicyCommonQuestionByPrimaryKey(id);
    }

    @Override
    public PageResult<PolicyCommonQuestion> findPolicyCommonQuestionListByCondition(PolicyCommonQuestionVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getSize());
        Map map = JSON.parseObject(JSON.toJSONString(vo), Map.class);
        List<PolicyCommonQuestion> policyCommonQuestionList = policyCommonQuestionRepository.searchPolicyCommonQuestionByCondition(map);
        PageInfo<PolicyCommonQuestion> pageInfo = new PageInfo<>(policyCommonQuestionList);
        return new PageResult<>(vo.getPage(), vo.getSize(), (int) pageInfo.getTotal(), policyCommonQuestionList);
    }

    @Override
    public void disableOrRelease(PolicyCommonQuestionVo2 vo) throws ArgumentErrorException {
        List<Long> ids = vo.getIds();
        for (Long id : ids) {
            String normalState  = "1";
            PolicyCommonQuestion question = policyCommonQuestionRepository.searchPolicyCommonQuestionByPrimaryKey(id.intValue());
            if (question.getState().equals(vo.getState())) {
                throw new ArgumentErrorException("相同状态无需修改");
            }
            question.setState(vo.getState());
            if (normalState.equals(vo.getState())) {
                question.setReleaseTime(DateTimeUtils.getCurDate("yyyy-MM-dd HH:mm:ss"));
            }

            policyCommonQuestionRepository.updatePolicyCommonQuestionByPrimaryKeySelective(question);
        }

    }

    @Override
    public void publishTimedCommonQuestion() {
        policyCommonQuestionRepository.publishTimedCommonQuestion();
    }

    @Override
    public PolicyCommonQuestion findPolicyCommonQuestionByIdForIndex(Long id) {
        PolicyCommonQuestion policyCommonQuestion = policyCommonQuestionRepository.searchPolicyCommonQuestionByPrimaryKey(id.intValue());
        policyCommonQuestionRepository.updatePolicyCommonQuestionViewsById(policyCommonQuestion.getId());
        return policyCommonQuestion;
    }

}
